package com.gxa.modules.app.outpatient.medicalrecord.controller;

import com.gxa.common.utils.Result;
import com.gxa.modules.app.outpatient.medicalrecord.service.MedicalRecordInquiryService;
import com.gxa.modules.model.entity.hospital.MedicalRecordInquiry;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者 yuanqiufeng
 * Date 2022/11/14
 */
@Api(tags = "病历查询")
@RestController
@RequestMapping("/medical-record")
public class MedicalRecordInquiryController {

    @Autowired
    private MedicalRecordInquiryService medicalRecordInquiryService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @ApiOperation(value = "按日期和患者ID查询病历")
    @GetMapping("/list/{patientId}/{startDate}/{endDate}")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = MedicalRecordInquiry.class)
    })
    public Result list(@PathVariable("patientId") Integer patientId,@PathVariable("startDate") Date startDate, @PathVariable("endDate") Date endDate){
        try{
            List<MedicalRecordInquiry> medicalRecordInquiryList = this.medicalRecordInquiryService.queryAll(patientId,startDate, endDate);
            return new Result().ok(medicalRecordInquiryList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败,数据异常");
        }
    }

    @ApiOperation(value = "按ID查询病历详情")
    @GetMapping("/list/{id}")
    @ApiResponses({
            @ApiResponse(code = 0,message = "success",response = MedicalRecordInquiry.class)
    })
    public Result queryById(@PathVariable("id") Integer id){
        try{
            MedicalRecordInquiry medicalRecordInquiry = this.medicalRecordInquiryService.queryById(id);
            return new Result().ok(medicalRecordInquiry);
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("查询失败,数据异常");
        }
    }


}
